{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n",
      "0 19 110 240 153 255\n"
     ]
    }
   ],
   "source": [
    "def empty(a):\n",
    "    pass\n",
    "\n",
    "def stackImages(scale,imgArray):\n",
    "    rows = len(imgArray)\n",
    "    cols = len(imgArray[0])\n",
    "    rowsAvailable = isinstance(imgArray[0], list)\n",
    "    width = imgArray[0][0].shape[1]\n",
    "    height = imgArray[0][0].shape[0]\n",
    "    if rowsAvailable:\n",
    "        for x in range ( 0, rows):\n",
    "            for y in range(0, cols):\n",
    "                if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:\n",
    "                    imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)\n",
    "                else:\n",
    "                    imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)\n",
    "                if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)\n",
    "        imageBlank = np.zeros((height, width, 3), np.uint8)\n",
    "        hor = [imageBlank]*rows\n",
    "        hor_con = [imageBlank]*rows\n",
    "        for x in range(0, rows):\n",
    "            hor[x] = np.hstack(imgArray[x])\n",
    "        ver = np.vstack(hor)\n",
    "    else:\n",
    "        for x in range(0, rows):\n",
    "            if imgArray[x].shape[:2] == imgArray[0].shape[:2]:\n",
    "                imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)\n",
    "            else:\n",
    "                imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)\n",
    "            if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)\n",
    "        hor= np.hstack(imgArray)\n",
    "        ver = hor\n",
    "    return ver\n",
    "\n",
    "\n",
    "\n",
    "path = './images/lambo.png'\n",
    "cv2.namedWindow(\"TrackBars\")\n",
    "cv2.resizeWindow(\"TrackBars\",640,240)\n",
    "cv2.createTrackbar(\"Hue Min\",\"TrackBars\",0,179,empty)\n",
    "cv2.createTrackbar(\"Hue Max\",\"TrackBars\",19,179,empty)\n",
    "cv2.createTrackbar(\"Sat Min\",\"TrackBars\",110,255,empty)\n",
    "cv2.createTrackbar(\"Sat Max\",\"TrackBars\",240,255,empty)\n",
    "cv2.createTrackbar(\"Val Min\",\"TrackBars\",153,255,empty)\n",
    "cv2.createTrackbar(\"Val Max\",\"TrackBars\",255,255,empty)\n",
    "\n",
    "while True:\n",
    "    img = cv2.imread(path)\n",
    "    imgHSV = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)\n",
    "    h_min = cv2.getTrackbarPos(\"Hue Min\",\"TrackBars\")\n",
    "    h_max = cv2.getTrackbarPos(\"Hue Max\", \"TrackBars\")\n",
    "    s_min = cv2.getTrackbarPos(\"Sat Min\", \"TrackBars\")\n",
    "    s_max = cv2.getTrackbarPos(\"Sat Max\", \"TrackBars\")\n",
    "    v_min = cv2.getTrackbarPos(\"Val Min\", \"TrackBars\")\n",
    "    v_max = cv2.getTrackbarPos(\"Val Max\", \"TrackBars\")\n",
    "    print(h_min,h_max,s_min,s_max,v_min,v_max)\n",
    "    lower = np.array([h_min,s_min,v_min])\n",
    "    upper = np.array([h_max,s_max,v_max])\n",
    "    mask = cv2.inRange(imgHSV,lower,upper)\n",
    "    imgResult = cv2.bitwise_and(img,img,mask=mask)\n",
    "\n",
    "\n",
    "    # cv2.imshow(\"Original\",img)\n",
    "    # cv2.imshow(\"HSV\",imgHSV)\n",
    "    # cv2.imshow(\"Mask\", mask)\n",
    "    # cv2.imshow(\"Result\", imgResult)\n",
    "\n",
    "    imgStack = stackImages(0.6,([img,imgHSV],[mask,imgResult]))\n",
    "    cv2.imshow(\"Stacked Images\", imgStack)\n",
    "\n",
    "    if cv2.waitKey(1) & 0xff == ord('q'):\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
